<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Market extends CI_Controller
{
	function Market()
	{
		parent::__construct();
        $this->load->model('common/common_model');
		$this->load->model('market/market_model');
        
        $this->load->library('Myinfo');
	}
	
	public function index()
	{
        if(!$this->session->userdata('username'))
        {
            redirect(site_url('login'));
            die();
        }	   
        $data = array();
        $userid = $this->session->userdata('uid');
        $user_oasis_orderid = $this->session->userdata('oasis_orderid');
        $c = (int)$this->input->get('c');
        if($c!=$user_oasis_orderid)
        {
            $oasisId = $this->common_model->ww_get_map_user_info($userid, $c);
            if($oasisId)
            {
                $this->session->set_userdata('oasis_orderid', $oasisId['oasis_orderid']);
            }
            else
            {
                redirect(site_url('home'));
                die();
            }
            $user_oasis_orderid = $oasisId['oasis_orderid'];
        }
        $data = $this->myinfo->loadUserinfo($userid, $user_oasis_orderid);
        /**/
        $sresource = $this->common_model->ww_get_send_resource_by_user($userid, $user_oasis_orderid);
        
        $data['chkSre'] = false;
        if($sresource)
        {
            $data['chkSre'] = true;
            $data['info'] = $sresource;
        }
		// header
		
		// content
		$this->load->view('market/market_view', $data);
		
		// footer
	}
    
    function review_coor_send()
    {
        if(!$this->session->userdata('username'))
        {
            redirect(site_url('login'));
            die();
        }
        $data = array();
        $userid = $this->session->userdata('uid');
        $user_oasis_orderid = $this->session->userdata('oasis_orderid');
        $rx = (int)$this->input->get('rootx');
        $ry = (int)$this->input->get('rooty');
        $info = $this->market_model->ww_get_field_around_coor($userid, $user_oasis_orderid, $rx, $ry);
        if($info)
        {
            $data['info'] = $info;
            $t_arrxy =  array(
                                'x' => $rx,
                                'y' => $ry
                            );
            $loc_user = $this->common_model->ww_get_map_user_info($userid, $user_oasis_orderid);
            $u_arrxy =  array(
                                'x' => $loc_user['x'],
                                'y' => $loc_user['y']
                            );
            $distanceTime = $this->mycommon->distanceTime($u_arrxy, $t_arrxy, 5, 400);
    
            $data['distance'] = gmdate('H:i:s', $distanceTime);
            $this->load->view('market/lst_field_view', $data);
        }
        else
        {
            echo 'NULL';
        }
    }
    
    function send()
    {
        if(!$this->session->userdata('username'))
        {
            redirect(site_url('login'));
            die();
        }
        
        $userid = $this->session->userdata('uid');
        $user_oasis_orderid = $coor = (int)$this->input->get('coor');
        $oasisId = $this->common_model->ww_get_map_user_info($userid, $coor);
        if(!$oasisId)
        {
            echo 'error';
            die();
        }
        $rx = intval($this->input->get('rootx', true));
        $ry = intval($this->input->get('rooty', true));
        $xx = intval($this->input->get('xx'));
        $yy = intval($this->input->get('yy'));
        $info = $this->market_model->ww_get_info_field_coor($rx, $ry, $xx, $yy);
        $mapuser = $this->common_model->ww_get_map_user_info($userid, $user_oasis_orderid);
        /*to*/
        $to_userid = $info['occupied'];
        $to_oasis_orderid = $info['oasis_orderid'];
        /*type*/
        $type = 0; // gửi thông thường(o to, tau thuy may bay).
        
        $distanceTime = $this->mycommon->distanceTime(array('x'=>$mapuser['x'], 'y'=>$mapuser['y']),array('x'=>$rx, 'y'=>$ry), 5, 400);
        
        $this->update_all_res_user($userid, $user_oasis_orderid);
        $res = $this->common_model->ww_get_res_user_info_by_field($userid, $user_oasis_orderid);
        
        $sfood = (int)$this->input->get('sfood');
        $swood = (int)$this->input->get('swood');
        $smetal = (int)$this->input->get('smetal');
        $spetro = (int)$this->input->get('spetro');
        
        $sfood = $sfood > $res['food'] ? $res['food'] : $sfood;
        $swood = $swood > $res['wood'] ? $res['wood'] : $swood;
        $smetal = $smetal > $res['metal'] ? $res['metal'] : $smetal;
        $spetro = $spetro > $res['petroleum'] ? $res['petroleum'] : $spetro;
        
        if($sfood>0 || $swood>0 || $smetal>0 || $spetro>0)
        {
            $this->market_model->ww_send_resoure($userid, $user_oasis_orderid, $to_userid, $to_oasis_orderid, $sfood, $swood, $smetal, $spetro, $distanceTime, $type);   
        }
        die();
    }
    
    function chk_send_resource_for_user($uid, $oasis_orderid)
    {
        /*
        * list all send resource has complete by user
        */
        
        $info = $this->common_model->ww_get_all_resource_from_user($uid, $oasis_orderid);
        foreach ($info as $lst)
        {
            $this->update_all_res_user($lst['to_uid'], $lst['to_oasis_orderid']);
            /*
            *  update from user -> to other user
            */
            
            $this->common_model->ww_update_all_resource_from_user($lst['lstid'], $lst['to_uid'], $lst['to_oasis_orderid'], $lst['tt_food'], $lst['tt_wood'], $lst['tt_metal'], $lst['tt_petro']);
        }
    }
}